package com.wtwd.campus.service.serviceImpl;

import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.Command;
import com.wtwd.campus.service.IRedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class RedisServiceImpl implements IRedisService {

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    private static final Logger logger = LoggerFactory.getLogger(RedisServiceImpl.class);

    @Override
    public void setKey(String key, Object value, long timeout, TimeUnit timeUnit) {
        ValueOperations<Object, Object> ops = redisTemplate.opsForValue();
        ops.set(key, value);
        if (timeout > 0)
            redisTemplate.boundValueOps(key).expire(timeout, timeUnit);
    }

    @Override
    public Object getValue(String key) {
        ValueOperations<Object, Object> ops = redisTemplate.opsForValue();
        return ops.get(key);
    }

    @Override
    public void deleteKey(String key) {
        redisTemplate.delete(key);
    }

    @Override
    public void saveCommand(String imei, String commandNumber, Object object) {
        //用指令前缀加上imei号存储一个map。map中存放的是每条指令的流水号为key，value是一个对象，对象中存放指令内容和状态
        String key = Constants.COMMAND_PREFIX + imei;
        logger.info("redis存储指令 imei={},commandNumber={},body={}", imei, commandNumber, object);
        Map<Object, Object> map = redisTemplate.opsForHash().entries(key);
        if (map == null) {
            map = new HashMap<>();
        }
        map.put(commandNumber, object);
        redisTemplate.opsForHash().putAll(Constants.COMMAND_PREFIX + imei, map);

    }

    @Override
    public Map<String, Object> getCommandsByImei(String imei) {
   /*     ValueOperations<Object, Object> ops = redisTemplate.opsForValue();
        Map<String,Object> commandMap = (Map<String,Object>)ops.get(Constants.COMMAND_PREFIX+imei);*/
        Object commandMap = redisTemplate.opsForHash().entries(Constants.COMMAND_PREFIX + imei);
        if (commandMap != null) {
            return (Map<String, Object>) commandMap;
        }
        return null;
    }

    @Override
    public boolean isDeviceOnline(String imei) {
        return redisTemplate.hasKey(Constants.DEVICE_ONLINE_PREFIX + imei);
    }

    @Override
    public void refreshDeviceOnlineTime(String imei, long timeout, TimeUnit timeUnit) {
        redisTemplate.boundValueOps(Constants.DEVICE_ONLINE_PREFIX + imei).expire(timeout, timeUnit);
    }

    @Override
    public void deleteCommand(String imei, String commandNumber) {
        String key = Constants.COMMAND_PREFIX + imei;
        Object map = redisTemplate.opsForHash().entries(key);
        logger.info("未删除指令前 redis map={}", map);
        if (map != null) {
            logger.info("删除前map的 hash值={}", ((Map) map).get(commandNumber).toString());
            logger.info("删除 redis指令 key={},index={} ", key, commandNumber);
            Long delete = redisTemplate.opsForHash().delete(key, commandNumber);
            logger.info("删除 redis指令 delete={}", delete);

        }
        logger.info("删除指令后 redis map={}", redisTemplate.opsForHash().entries(key));
    }

    @Override
    public void updateCommand(String imei, String commandNumber, Command command) {
        redisTemplate.opsForHash().put(imei, commandNumber, command);
    }

    @Override
    public Map<String, Object> getUnInStorageDevice() {
        Object commandMap = redisTemplate.opsForHash().entries(Constants.UN_STORAGE_DEVICE);
        if (commandMap != null) {
            return (Map<String, Object>) commandMap;
        }
        return null;
    }
}
